import data from '../data/emoji-data.js'


if (!Object.values) Object.values = function (obj) {
    if (obj !== Object(obj))
        throw new TypeError('Object.values called on a non-object');
    let val = [], key;
    for (key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            val.push(obj[key]);
        }
    }
    return val;
};

let emojiData = {};

Object.values(data).forEach(item => {
    emojiData = $.extend(emojiData, item);
});





/**
 *
 *
 * @export
 * @param {string} value
 * @returns {string}
 */

export function emoji(value) {
    if (!value) return;
    Object.keys(emojiData).forEach(item => {
        while(value.indexOf(item) > -1)
        value = value.replace(item, createIcon(item));
    });
    return value
}

function createIcon(item) {
        const value = emojiData[item];
        let reg = /(http):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
        if(reg.test(value)){
            return `<img src=${value} width="24px" height="24px">`    
        }else{
            const path = './static/emoji/'
            return `<img src=${path}${value} width="24px" height="24px">`
    
        }
}
